package csel.view;

import java.awt.image.*;
import java.awt.Point;
import java.io.File;
import java.awt.Graphics;
import java.io.IOException;
import javax.imageio.ImageIO;

import csel.model.Entity;
import csel.model.Direction;
import csel.model.GameObject;

public class ObjectRepresentor {
	
	private BufferedImage img;
	private final GameObject myObject;
    private final int width;
    private final int height;
    private final int unitheight;
    
    public ObjectRepresentor(GameObject myObject)
    {
    		if (myObject == null)
    		{
    			this.myObject = null;
    			try
    			{
    			img = ImageIO.read(new File("images/null.png"));
    			}
    			catch (IOException e)
    			{
    			}
    			width = 0;
    			height = 0;
    			unitheight = 0;
    		}
    		else
    		{
    			String imgID = myObject.getTypeId();
    			try
    			{
    				img = ImageIO.read(new File("images/"+imgID+".png"));
    			}
    			catch (IOException e)
    			{
    			}
    			this.myObject = myObject;
    			width = img.getWidth();
    			height = img.getHeight();
    			unitheight = height/8;
    		}
    }
    
    public void draw(Graphics g, Point p)
    {
    	if (myObject == null)
    		return;
    	
    	int startPoint=0;
    	Direction d = myObject.getDirection();	

    	if (d == Direction.NORTH)
    		startPoint = 160;
    	else if (d == Direction.NORTHEAST)
    		startPoint = 200;
    	else if (d == Direction.EAST)
    		startPoint = 240;
    	else if (d == Direction.SOUTHEAST)
    		startPoint = 280;
    	else if (d == Direction.SOUTH)
    		startPoint = 0;
    	else if (d == Direction.SOUTHWEST)
    		startPoint = 40;
    	else if (d == Direction.WEST)
    		startPoint = 80;
    	else
    		startPoint = 120;

    	int x = (int)p.getX();
    	int y = (int)p.getY();
   
    	g.drawImage(img, x, y, x+width, y+unitheight, 0, startPoint, 40, startPoint+40, null);
    }
    
    public BufferedImage getSimpleImage()
    {
        return img.getSubimage(0,0,width,unitheight);
    }

}
